package com.fx.zmlzml.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 错误码枚举类
 * 集中管理系统中所有的错误码和对应的默认错误消息
 */
@Getter
@AllArgsConstructor
public enum ErrorEnum {
    
    // ========== 系统级错误码 ==========
    SYSTEM_ERROR(500, "系统内部错误"),
    DATABASE_ERROR(501, "数据库操作异常"),
    NETWORK_ERROR(502, "网络连接异常"),
    NULL_POINTER_ERROR(503, "空指针异常"),
    ARITHMETIC_ERROR(504, "算术计算异常"),
    CLASS_CAST_ERROR(505, "类型转换异常"),
    ILLEGAL_STATE(506, "非法状态异常"),
    ILLEGAL_ARGUMENT(507, "非法参数异常"),
    
    // ========== 参数相关错误码 ==========
    PARAM_INVALID(400, "请求参数无效"),
    PARAM_MISSING(401, "请求参数缺失"),
    PARAM_TYPE_ERROR(402, "请求参数类型错误"),
    PARAM_VALIDATION_ERROR(403, "请求参数验证失败"),
    PARAM_CONSTRAINT_VIOLATION(404, "参数约束违反"),
    MISSING_PARAMETER(405, "缺失请求参数"),
    PARAM_BIND_ERROR(406, "参数绑定异常"),
    
    // ========== 资源相关错误码 ==========
    RESOURCE_NOT_FOUND(404, "请求的资源不存在"),
    RESOURCE_ALREADY_EXISTS(405, "资源已存在"),
    
    // ========== 业务相关错误码 ==========
    // 电脑主板相关错误码
    MOTHERBOARD_ADD_FAILED(1001, "添加电脑主板失败"),
    MOTHERBOARD_UPDATE_FAILED(1002, "更新电脑主板失败"),
    MOTHERBOARD_DELETE_FAILED(1003, "删除电脑主板失败"),
    MOTHERBOARD_NOT_FOUND(1004, "请求的电脑主板不存在"),
    MOTHERBOARD_QUERY_FAILED(1005, "查询电脑主板失败"),
    
    // 电脑CPU相关错误码
    CPU_ADD_FAILED(2001, "添加电脑CPU失败"),
    CPU_UPDATE_FAILED(2002, "更新电脑CPU失败"),
    CPU_DELETE_FAILED(2003, "删除电脑CPU失败"),
    CPU_NOT_FOUND(2004, "请求的电脑CPU不存在"),
    CPU_QUERY_FAILED(2005, "查询电脑CPU失败"),
    
    // 电脑显卡相关错误码
    GPU_ADD_FAILED(3001, "添加电脑显卡失败"),
    GPU_UPDATE_FAILED(3002, "更新电脑显卡失败"),
    GPU_DELETE_FAILED(3003, "删除电脑显卡失败"),
    GPU_NOT_FOUND(3004, "请求的电脑显卡不存在"),
    GPU_QUERY_FAILED(3005, "查询电脑显卡失败"),
    
    // ========== 权限相关错误码 ==========
    UNAUTHORIZED(401, "未授权访问"),
    FORBIDDEN(403, "禁止访问"),
    
    // ========== HTTP请求相关错误码 ==========
    METHOD_NOT_ALLOWED(405, "不支持的请求方法"),
    UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型"),
    MESSAGE_NOT_READABLE(400, "HTTP消息不可读"),
    NOT_ACCEPTABLE(406, "不支持的内容类型"),
    REQUEST_TIMEOUT(408, "请求超时"),
    CONFLICT(409, "请求冲突"),
    GONE(410, "资源已不存在"),
    LENGTH_REQUIRED(411, "需要Content-Length"),
    PRECONDITION_FAILED(412, "前置条件失败"),
    PAYLOAD_TOO_LARGE(413, "请求体过大"),
    URI_TOO_LONG(414, "请求URI过长"),
    REQUESTED_RANGE_NOT_SATISFIABLE(416, "请求范围不满足"),
    EXPECTATION_FAILED(417, "期望失败"),
    UNPROCESSABLE_ENTITY(422, "无法处理的实体"),
    TOO_MANY_REQUESTS(429, "请求过多"),
    NOT_IMPLEMENTED(501, "未实现"),
    BAD_GATEWAY(502, "错误的网关"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),
    HTTP_VERSION_NOT_SUPPORTED(505, "不支持的HTTP版本"),
    
    // ========== 数据相关错误码 ==========
    DATA_INTEGRITY_VIOLATION(409, "数据完整性违反"),
    
    // ========== 通用业务错误码 ==========
    BUSINESS_ERROR(10000, "业务处理失败");
    
    private final int code; // 错误码
    private final String message; // 默认错误消息
    
    /**
     * 根据错误码获取对应的错误枚举
     * @param code 错误码
     * @return 错误枚举，如果没有找到对应错误码则返回SYSTEM_ERROR
     */
    public static ErrorEnum getByCode(int code) {
        for (ErrorEnum errorEnum : ErrorEnum.values()) {
            if (errorEnum.getCode() == code) {
                return errorEnum;
            }
        }
        return SYSTEM_ERROR;
    }
}